package com.zhangxu.microservice.system.web.controller.ae;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.system.biz.dto.RoleDTO;
import com.zhangxu.microservice.system.biz.dto.UpdateRoleDTO;
import com.zhangxu.microservice.system.biz.service.RoleService;
import com.zhangxu.microservice.system.dao.condition.RoleCondition;
import com.zhangxu.microservice.system.dao.enums.RoleStatusEnum;
import com.zhangxu.microservice.system.web.convert.RoleWebConverter;
import com.zhangxu.microservice.system.web.request.CreateRoleReq;
import com.zhangxu.microservice.system.web.request.PageReq;
import com.zhangxu.microservice.system.web.request.UpdateRoleReq;
import com.zhangxu.microservice.system.web.response.PageResponse;
import com.zhangxu.microservice.system.web.response.RoleResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色管理控制器
 */
@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/ae/roles")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;
    private final RoleWebConverter roleWebConverter;

    @Operation(summary = "创建角色", description = "创建新角色")
    @PostMapping
    public CommonResult<Long> createRole(@RequestBody @Validated CreateRoleReq createRoleReq) {
        Long roleId = roleService.createRole(roleWebConverter.toCreateRoleDTO(createRoleReq));
        return CommonResult.success(roleId);
    }

    @Operation(summary = "更新角色", description = "更新现有角色信息")
    @PutMapping("/{roleId}")
    public CommonResult<Void> updateRole(@Parameter(description = "角色ID", required = true, example = "1") @PathVariable("roleId") Long roleId,
                                         @RequestBody @Validated UpdateRoleReq createRoleReq) {
        UpdateRoleDTO updateRoleDTO = roleWebConverter.toUpdateRoleDTO(createRoleReq);
        updateRoleDTO.setId(roleId);
        roleService.updateRole(updateRoleDTO);
        return CommonResult.success();
    }

    @Operation(summary = "删除角色", description = "根据ID删除角色")
    @DeleteMapping("/{roleId}")
    public CommonResult<Void> deleteRole(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable("roleId") Long roleId) {
        roleService.deleteRole(roleId);
        return CommonResult.success();
    }

    @Operation(summary = "启用角色", description = "启用指定角色")
    @PostMapping("/{roleId}/enable")
    public CommonResult<Void> enableRole(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable("roleId") Long roleId) {
        roleService.enableRole(roleId);
        return CommonResult.success();
    }

    @Operation(summary = "禁用角色", description = "禁用指定角色")
    @PostMapping("/{roleId}/disable")
    public CommonResult<Void> disableRole(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable("roleId") Long roleId) {
        roleService.disableRole(roleId);
        return CommonResult.success();
    }

    @Operation(summary = "获取角色详情", description = "根据ID获取角色详情")
    @GetMapping("/{roleId}")
    public CommonResult<RoleResp> getRoleById(
            @Parameter(description = "角色ID", required = true, example = "1")
            @PathVariable("roleId") Long roleId) {
        RoleDTO roleDTO = roleService.getRoleById(roleId);
        return CommonResult.success(roleWebConverter.toRoleResp(roleDTO));
    }

    @Operation(summary = "角色列表", description = "分页查询角色列表")
    @GetMapping
    public CommonResult<PageResponse<RoleResp>> rolePage(
            @Valid PageReq pageReq,
            @Parameter(description = "角色名称") @RequestParam(value = "name", required = false) String name,
            @Parameter(description = "角色编码") @RequestParam(value = "code", required = false) String code,
            @Parameter(description = "状态（ENABLE-启用，DISABLE-禁用）") @RequestParam(value = "status", required = false) RoleStatusEnum status) {
        RoleCondition condition = new RoleCondition();
        condition.setName(name);
        condition.setCode(code);
        condition.setStatus(status);
        condition.setPageNum(pageReq.getPageNum());
        condition.setPageSize(pageReq.getPageSize());
        IPage<RoleDTO> page = roleService.rolePage(condition);
        return CommonResult.success(PageResponse.convert(page, roleWebConverter::toRoleResp));
    }
}